<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body class="article-body">
<div class="article-intro">

    <h1>Java 封装</h1>
    <hr>

    <p>在面向对象程式设计方法中，封装（英语：Encapsulation）是指一种将抽象性函式接口的实现细节部份包装、隐藏起来的方法。
    </p>
    <p>封装可以被认为是一个保护屏障，防止该类的代码和数据被外部类定义的代码随机访问。</p><p>
    要访问该类的代码和数据，必须通过严格的接口控制。</p><p>
    封装最主要的功能在于我们能修改自己的实现代码，而不用修改那些调用我们代码的程序片段。</p><p>
    适当的封装可以让程式码更容易理解与维护，也加强了程式码的安全性。</p>
    <h3>封装的优点</h3>
    <ul>

        <li><p>
            1. 良好的封装能够减少耦合。</p></li><li><p>
        2. 类内部的结构可以自由修改。</p></li><li><p>
        3. 可以对成员变量进行更精确的控制。</p></li><li><p>
        4. 隐藏信息，实现细节。</p></li></ul>


    <hr>
    <h2>实现Java封装的步骤</h2>
    <p>1. 修改属性的可见性来限制对属性的访问（一般限制为private），例如：</p>
    <div class="example">
        <div class="example_code">
            <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Person</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">;
    </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">age</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></pre>
        </div>
    </div>
    <p>
        这段代码中，将 <strong>name</strong> 和 <strong>age</strong> 属性设置为私有的，只能本类才能访问，其他类都访问不了，如此就对信息进行了隐藏。
    </p><p>2. 对每个值属性提供对外的公共方法访问，也就是创建一对赋取值方法，用于对私有属性的访问，例如：</p>
    <div class="example">
        <div class="example_code">
            <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Person</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">;
    </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">age</span><span class="hl-code">;
​
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">getAge</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">age</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
​
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">getName</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
​
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setAge</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">age</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">age</span><span class="hl-code"> = </span><span class="hl-identifier">age</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
​
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setName</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-code"> = </span><span class="hl-identifier">name</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
        </div>
    </div>
    <p>采用 <strong>this</strong> 关键字是为了解决实例变量（private String name）和局部变量（setName(String name)中的name变量）之间发生的同名的冲突。</p>
    <hr>
    <h2>实例</h2>
    <p>让我们来看一个java封装类的例子：</p>
    <div class="example">
        <h2 class="example">EncapTest.java 文件代码：</h2>
        <div class="example_code">
            <pre class="hl-main"><span class="hl-comment">/*</span><span class="hl-comment"> 文件名: EncapTest.java </span><span class="hl-comment">*/</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">EncapTest</span><span class="hl-brackets">{</span><span class="hl-code">

   </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">;
   </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">idNum</span><span class="hl-code">;
   </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">age</span><span class="hl-code">;

   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">getAge</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">age</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">

   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">getName</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">

   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">getIdNum</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">idNum</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">

   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setAge</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">newAge</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">age</span><span class="hl-code"> = </span><span class="hl-identifier">newAge</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">

   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setName</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">newName</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">name</span><span class="hl-code"> = </span><span class="hl-identifier">newName</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">

   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">setIdNum</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">newId</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">idNum</span><span class="hl-code"> = </span><span class="hl-identifier">newId</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
        </div>
    </div>
    <p>以上实例中public方法是外部类访问该类成员变量的入口。</p><p>
    通常情况下，这些方法被称为getter和setter方法。</p><p>
    因此，任何要访问类中私有成员变量的类都要通过这些getter和setter方法。</p><p>
    通过如下的例子说明EncapTest类的变量怎样被访问：</p>
    <div class="example">
        <h2 class="example">RunEncap.java 文件代码：</h2>
        <div class="example_code">
            <pre class="hl-main"><span class="hl-comment">/*</span><span class="hl-comment"> F文件名 : RunEncap.java </span><span class="hl-comment">*/</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">RunEncap</span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">EncapTest</span><span class="hl-code"> </span><span class="hl-identifier">encap</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">EncapTest</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">encap</span><span class="hl-code">.</span><span class="hl-identifier">setName</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">James</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">encap</span><span class="hl-code">.</span><span class="hl-identifier">setAge</span><span class="hl-brackets">(</span><span class="hl-number">20</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">encap</span><span class="hl-code">.</span><span class="hl-identifier">setIdNum</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">12343ms</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;

      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Name : </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">encap</span><span class="hl-code">.</span><span class="hl-identifier">getName</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">+
                             </span><span class="hl-quotes">"</span><span class="hl-string"> Age : </span><span class="hl-quotes">"</span><span class="hl-code">+ </span><span class="hl-identifier">encap</span><span class="hl-code">.</span><span class="hl-identifier">getAge</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
        </div>
    </div>
    <p>以上代码编译运行结果如下:</p>
    <pre class="prettyprint prettyprinted" style=""><span class="typ">Name</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="typ">James</span><span class="pln"> </span><span class="typ">Age</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="lit">20</span></pre>

</div>
</body>
</html>
